﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

 using System.IO;

using System.Web.Razor;
using System.Web.Razor.Generator;
using System.Web.Razor.Parser;

namespace STX.Core.TemplateEngin
{
    public abstract class TemplateBase
    {
        #region 字段
        [ThreadStatic]
        private static StringBuilder builder;
        #endregion

        #region 属性

        public string Layout { get; set; }

        public StringBuilder Builder
        {
            get { return builder ?? (builder = new StringBuilder()); }
        }

        #endregion

        #region 方法
        public virtual void Execute(){}


        /// <summary>
        /// Writes the specified object to the template result.
        /// </summary>
        /// <param name="object">The object to write.</param>
        public void Write(object @object)
        {
            if (@object == null)
                return;

            Builder.Append(@object);
        }

        /// <summary>
        /// Writes the specified string to the template result.
        /// </summary>
        /// <param name="string">The string to write.</param>
        public void WriteLiteral(string @string)
        {
            if (@string == null)
                return;

            Builder.Append(@string);
        }

        /// <summary>
        /// Writes a string literal to the specified <see cref="TextWriter"/>.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="literal">The literal to be written.</param>
        public static void WriteLiteralTo(TextWriter writer, string literal)
        {
            if (literal == null)
                return;

            writer.Write(literal);
        }

        /// <summary>
        /// Writes the specified object to the specified <see cref="TextWriter"/>.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="obj">The object to be written.</param>
        public static void WriteTo(TextWriter writer, object obj)
        {
            if (obj == null)
                return;

            writer.Write(obj);
        }

        #endregion
    }
}
